window.addEventListener("load", function (event) {
    let oReq = new XMLHttpRequest();
    oReq.open('GET', '/config');
    function reqListener () {
        let config = JSON.parse(this.responseText);
        let ssid = document.getElementById("ssid");
        if(ssid && config.ssid)
            ssid.value = config.ssid;

        let pass = document.getElementById("pass");
        if(pass && config.pass)
            pass.value = config.pass;

        let server = document.getElementById("server");
        if(server && config.ws_addr)
            server.value = config.ws_addr;

        let token = document.getElementById("clientToken");
        if(token && config.token)
            token.value = config.token;

        let ble_name = document.getElementById("ble_name");
        if(ble_name && config.ble_name)
            ble_name.value = config.ble_name;

        let ble_max_conn = document.getElementById("ble_max_conn");
        if(ble_max_conn && config.ble_max_conn)
            ble_max_conn.value = config.ble_max_conn;
        
        let gw_mac = document.getElementById("gw_mac");
        if(gw_mac && config.gw_mac)
            gw_mac.innerText  = 'Gateway MAC address : ' + config.gw_mac;
        
        let gw_version = document.getElementById("gw_version");
        if(gw_version && config.gw_version)
            gw_version.innerText = 'Gateway version : ' +config.gw_version;
        
        let gw_ip = document.getElementById("gw_ip");
        if(gw_ip && config.gw_ip)
            gw_ip.innerText = 'Gateway IP : '+config.gw_ip;

        let EnableDetailLog = document.getElementById("enable_log");
        if(EnableDetailLog && config.detail_log)
            document.getElementById("enable_log").checked=true
        else
            document.getElementById("enable_log").checked=false
           
        let EnableBase64 = document.getElementById("enable_base64");
        if(EnableBase64 && config.base64)
            document.getElementById("enable_base64").checked=true
        else
            document.getElementById("enable_base64").checked=false

        //正确做法在下面 fileUploadHandle
        //document.getElementById("ota_button").disabled=true
        //console(document)
        //document.getElementById("ota_button").setAttribute("disabled", true);//设置不可点击
        //document.getElementById("ota_button").style.backgroundColor  = '#555555';//设置背景色
        //document.getElementById("ota_button").removeAttribute("disabled");//去掉不可点击
    }
    oReq.addEventListener("load", reqListener);
    oReq.send();
});
document.getElementById('wifisave').addEventListener("click", function() {
    let ssid = document.getElementById("ssid").value;
    ssid = encodeURIComponent(ssid)
    let pass = document.getElementById("pass").value;
    pass = encodeURIComponent(pass)
    let param = 'ssid=' + ssid + '&pass=' + pass;
    console.log(param);
    let oReq = new XMLHttpRequest();
    oReq.open('GET', '/wifi?'+param);
    function reqListener () {
       alert(this.responseText);
      }
      
      oReq.addEventListener("load", reqListener);
      oReq.send();
});

// WebSocket
document.getElementById('wsSave').addEventListener("click", function() {
    let ws_addr = document.getElementById("server").value;
    let token = document.getElementById("clientToken").value;
    let param = 'ws_addr=' + ws_addr + '&token=' + token;
    console.log(param);
    let oReq = new XMLHttpRequest();
    oReq.open('GET', '/ws?'+param);
    function reqListener () {
       alert(this.responseText);
    }
    oReq.addEventListener("load", reqListener);
    oReq.send();
});
//ble setting
document.getElementById('bleSave') && document.getElementById('bleSave').addEventListener("click", function() {
    let name = document.getElementById("ble_name").value;
    name = encodeURIComponent(name)
    let conn = document.getElementById("ble_max_conn").value;
    let param = 'ble_name=' + name + '&ble_max_conn=' + conn;
    console.log(param);
    let oReq = new XMLHttpRequest();
    oReq.open('GET', '/ble_setting?'+param);
    function reqListener () {
       alert(this.responseText);
    }
    oReq.addEventListener("load", reqListener);
    oReq.send();
});
//config save
document.getElementById('rmSave') && document.getElementById('rmSave').addEventListener("click", function() {
    let EnableDetailLog = 0;
    let EnableBase64    = 0;
    if(document.getElementById("enable_log").checked) EnableDetailLog=1;
    if(document.getElementById("enable_base64").checked) EnableBase64=1;
    let param = 'detail_log=' + EnableDetailLog + '&base64=' + EnableBase64;
    console.log(param);
    let oReq = new XMLHttpRequest();
    oReq.open('GET', '/rm_setting?'+param);
    function reqListener () {
       alert(this.responseText);
    }
    oReq.addEventListener("load", reqListener);
    oReq.send();
});

document.getElementById('otaButton').addEventListener('click',function(){
    fileUploadHandle();
})

document.getElementById('upload').addEventListener('change',function(){
    var fileAllPath = this.value;
    var filesplit = fileAllPath.split("\\");
    document.getElementById('viewfile').value = filesplit[filesplit.length-1] ;
})


function fileUploadHandle(){
    var button = document.querySelector("#otaButton"),input = document.querySelector("#upload");
    if (input.files.length === 0) {
		console.log("No File Input");//鼠标-检查-仿真显示
        alert("No File Input")//UI弹窗显示
		return;
	}
    console.log("fileUploadHandle")

    var formData = new FormData();
	formData.append("uploadfile", input.files[0]);

	var oReq = new XMLHttpRequest();

    button.setAttribute("disabled",true);
    button.style.backgroundColor  = '#555555';
    cocoMessage.info("Uploading......", 0);

    oReq.onreadystatechange = function() {
		if (oReq.readyState === 4 && (oReq.status === 200||oReq.status === 0)) {
            //WIN10--200 LINUX--0
            //https://blog.csdn.net/E78790602/article/details/102388119?spm=1035.2023.3001.6557&utm_medium=distribute.pc_relevant_bbs_down_v2.none-task-blog-2~default~OPENSEARCH~Rate-2.pc_relevant_bbs_down_cate&depth_1-utm_source=distribute.pc_relevant_bbs_down_v2.none-task-blog-2~default~OPENSEARCH~Rate-2.pc_relevant_bbs_down_cate
            console.log("oReq.status=%d",oReq.status)
            button.removeAttribute("disabled");
            button.style.backgroundColor  = '#d33';
            cocoMessage.destroyAll();
            alert(this.responseText);
            //alert("Done");
		}
	};

	oReq.open("POST", "/upload");
    oReq.send(formData);
}


